﻿using AopDLL;
using DBModel.Context;
using DBModel.Entity;
using IDBLayer.Interface;
using ILogicLayer.DTO;
using ILogicLayer.Interface;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace LogicLayer.Service
{
    public class UserService : IUserService
    {
        private readonly IRepository<AppDBContext, BasicUserInfo> rep;

        public UserService(IRepository<AppDBContext, BasicUserInfo> repo)
        {
            rep = repo;
        }

        public async Task<bool> AddAsync(List<BasicUserInfo> list)
        {
            return await rep.AddListAsync(list.ToArray()) == list.Count;
        }

        public async Task<bool> DeleteAsync(Guid id)
        {
            return await rep.DeleteAsync(new BasicUserInfo { Id = id }) > 0;
        }

        public async Task<ResultDTO<BasicUserInfo>> GetAll(int pageIndex, int pageSize)
        {
            object sum = null;
            List<BasicUserInfo> list = await rep.GetListAsync<int>(null, pageIndex, pageSize, sum);
            return new ResultDTO<BasicUserInfo>(list, sum, pageIndex, pageSize);
        }

        [DothingAfterInterceptor]
        public async Task<List<BasicUserInfo>> GetAsync(string nickName)
        {
            object num = new object();
            return await rep.GetListAsync<DateTime>(w => w.Nickname.Equals(nickName), 0, byte.MaxValue, num,
                s => s.CreateTime, "DESC", s => new BasicUserInfo
                {
                    Name = s.Name,
                    State = s.State,
                    Photo = s.Photo,
                    Phone = s.Phone,
                    CreateTime = s.CreateTime
                });
        }

        public async Task<bool> UpdateAsync(BasicUserInfo user)
        {
            return await rep.ModifyAsync(user) > 0;
        }

    }
}